

#include "color_LED.h"
#include "gd32f30x_timer.h"

STR_LED_DUTY  LED_DUTY[NUM_COLOR_LED];
STR_RGB_VALUE LED_RGB = {.XRGB = 255};

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/


void LED_duty_set(STR_RGB_VALUE *rgb, STR_LED_DUTY *duty)
{
	
	uint32_t rgb_set = rgb->XRGB;
	
	
	for(uint8_t i=0; i<24; i++)
	{
		if((( rgb_set >> i) & 0x00000001) == 1)
		{
			duty->duty[23 - i] = LED_PWM_DUTY_H;
		}
		else
		{
			duty->duty[23 - i] = LED_PWM_DUTY_L;
		}
	}
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/


void LED_RGB_set_ALL(STR_RGB_VALUE *rgb, uint8_t LED_num)
{
	if(LED_num > NUM_COLOR_LED)
	{
		return;
	}
	else
	{
		STR_LED_DUTY duty_temp;
		
		LED_duty_set(rgb, &duty_temp);
		
		for(uint8_t i=0; i<LED_num; i++)
		{
			for(uint8_t j=0; j<8; j++)
			{
				LED_DUTY[i].R_duty[j] = duty_temp.R_duty[j];
				LED_DUTY[i].G_duty[j] = duty_temp.G_duty[j];
				LED_DUTY[i].B_duty[j] = duty_temp.B_duty[j];
			}
		}
	}
}







/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void LED1_dma_sendMessg(STR_LED_DUTY *dutyQueue, uint16_t led_num)
{
	if((SET == dma_flag_get(DMA1, DMA_CH0, DMA_FLAG_FTF)) || 
						(0 == dma_transfer_number_get(DMA1, DMA_CH0)))
	{
		
		timer_dma_transfer_config(TIMER7, TIMER_DMACFG_DMATA_CH2CV, TIMER_DMACFG_DMATC_1TRANSFER); 
		
		dma_channel_disable(DMA1, DMA_CH0);
		dma_flag_clear(DMA1, DMA_CH0, DMA_FLAG_FTF);
		dma_memory_address_config(DMA1, DMA_CH0, (uint32_t)dutyQueue[0].duty);
		dma_transfer_number_config(DMA1, DMA_CH0, led_num * 24);		
		
		dma_channel_enable(DMA1, DMA_CH0);

		timer_enable(TIMER7);
	}
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void LED2_dma_sendMessg(STR_LED_DUTY *dutyQueue, uint16_t led_num)
{
	if((SET == dma_flag_get(DMA1, DMA_CH1, DMA_FLAG_FTF)) || 
						(0 == dma_transfer_number_get(DMA1, DMA_CH1)))
	{
		
		timer_dma_transfer_config(TIMER7, TIMER_DMACFG_DMATA_CH3CV, TIMER_DMACFG_DMATC_1TRANSFER); 
		
		dma_channel_disable(DMA1, DMA_CH1);
		dma_flag_clear(DMA1, DMA_CH1, DMA_FLAG_FTF);
		dma_memory_address_config(DMA1, DMA_CH1, (uint32_t)dutyQueue[0].duty);
		dma_transfer_number_config(DMA1, DMA_CH1, led_num * 24);		
		
		dma_channel_enable(DMA1, DMA_CH1);

		timer_enable(TIMER7);
	}
}





/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void LED_color_ctrl(ENUM_LED_TYPE type, uint8_t blink_time)
{
	static uint8_t cnt = 0;
	
	switch(type)
	{
		case LED_COLOR_OFF:
			
			LED_RGB.R = 0;
			LED_RGB.G = 0;
			LED_RGB.B = 0;
			
			break;
		
		case LED_COLOR_GREEN:
			
			LED_RGB.R = 0;
			LED_RGB.G = 255;
			LED_RGB.B = 0;
		
			break;
		
		case LED_COLOR_RED:
			
			LED_RGB.R = 255;
			LED_RGB.G = 0;
			LED_RGB.B = 0;
		
			break;
		
		case LED_COLOR_BLUE:
			
			LED_RGB.R = 0;
			LED_RGB.G = 0;
			LED_RGB.B = 255;
		
			break;
		
		case LED_COLOR_ORANGE:
			
			LED_RGB.R = 255;
			LED_RGB.G = 153;
			LED_RGB.B = 0;
		
			break;
		
		case LED_COLOR_PURPLE:
			
			LED_RGB.R = 255;
			LED_RGB.G = 0;
			LED_RGB.B = 200;
		
			break;
		
		case LED_COLOR_WHITE:
			
			LED_RGB.R = 255;
			LED_RGB.G = 255;
			LED_RGB.B = 255;
		
			break;
	}
	
	LED_RGB_set_ALL(&LED_RGB, NUM_COLOR_LED);
	
	if(cnt % 2)
	{
		LED1_dma_sendMessg(&LED_DUTY[0], NUM_COLOR_LED + 1);
	}
	else
	{
		LED2_dma_sendMessg(&LED_DUTY[0], NUM_COLOR_LED + 1);
	}
	
	cnt++;	
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void DMA1_Channel0_IRQHandler(void)
{
	
	if(dma_interrupt_flag_get(DMA1, DMA_CH0, DMA_INT_FLAG_FTF))
    {
        dma_interrupt_flag_clear(DMA1, DMA_CH0, DMA_INT_FLAG_FTF);    
        dma_channel_disable(DMA1, DMA_CH0);
	    
		timer_disable(TIMER7);
		timer_channel_output_pulse_value_config(TIMER7, TIMER_CH_2, 0);		
    }
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void DMA1_Channel1_IRQHandler(void)
{
	
	if(dma_interrupt_flag_get(DMA1, DMA_CH1, DMA_INT_FLAG_FTF))
    {
        dma_interrupt_flag_clear(DMA1, DMA_CH1, DMA_INT_FLAG_FTF);    
        dma_channel_disable(DMA1, DMA_CH1);
	    
		timer_disable(TIMER7);
		timer_channel_output_pulse_value_config(TIMER7, TIMER_CH_3, 0);			
    }
}





